package com.venue.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import com.venue.constant.TimeConstant;
import com.venue.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.venue.dao.PLAdao;
import com.venue.response.Response;
import com.venue.util.BeanJsonUtil;
import com.venue.web.websocket.PLAwebSocket;

@Service
public class PLAService {


	@Autowired
	private PLAdao PLAdao;

	/**
	 * 热力图
	 * room = -1全部场馆 
	 * room = 4 4号管数据
	 * @return
	 */
	public String headMapData(String room,String realTime){
		List list = new ArrayList();
		if("-1".equals(room)){
			Map dataMap1 = new HashMap();
			List<HeatMapData> list_hm1 = new ArrayList<HeatMapData>();
			String room1 = "4.1";
			if("".equals(realTime)) {
				TimeConstant.realTime = PLAdao.getRealTime(room1, getTime());
				list_hm1 = PLAdao.headMapData(room1, TimeConstant.realTime);
			}else{
				list_hm1 = PLAdao.headMapData(room1, realTime);
			}
			dataMap1.put("headMapData", list_hm1);
			dataMap1.put("roomCode", room1);

			Map dataMap2 = new HashMap();
			List<HeatMapData> list_hm2 = new ArrayList<HeatMapData>();
			String room2 = "4.2";
			if("".equals(realTime)) {
				list_hm2 = PLAdao.headMapData(room2, TimeConstant.realTime);
			}else{
				list_hm2 = PLAdao.headMapData(room2, realTime);
			}
			dataMap2.put("headMapData", list_hm2);
			dataMap2.put("roomCode", room2);
			list.add(dataMap1);
			list.add(dataMap2);
		}else {
			Map dataMap = new HashMap();
			List<HeatMapData> list_hm = new ArrayList<HeatMapData>();
			if("".equals(realTime)) {
				TimeConstant.realTime = PLAdao.getRealTime(room, getTime());
				list_hm = PLAdao.headMapData(room, TimeConstant.realTime);
			}else{
				list_hm = PLAdao.headMapData(room, realTime);
			}
			dataMap.put("headMapData", list_hm);
			dataMap.put("roomCode", room);
			list.add(dataMap);
		}
		return getMessage(list,"headMapData");
	}

	public String getTime(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar beforeTime = Calendar.getInstance();
		beforeTime.add(Calendar.MINUTE, -5);// 3分钟之前的时间
		Date before = beforeTime.getTime();
		return format.format(before);
	}

	public String getDate(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar beforeTime = Calendar.getInstance();
		beforeTime.add(Calendar.MINUTE, -60);// 3分钟之前的时间
		Date before = beforeTime.getTime();
		return format.format(before);
	}

	public String getRealTime(){
		String realTime = PLAdao.getRealTime("4.1",getTime());
		return realTime;
	}

	public String getHourTime(){
		String dateTime = PLAdao.getHourTime("4.1",getDate());
		return dateTime;
	}
	/**
	 * 发送热力图
	 * @param room
	 */
	public void sendHeadMapData(String room,String realTime){
		sendMessage(headMapData(room,realTime));
	}
	
	/**
	 * 场馆来源
	 * room = -1所有
	 * @return
	 */
	public String fromWhere(String room,String dateTime){
		List<CommonMapData> list = new ArrayList<CommonMapData>();


		if("-1".equals(room)){
			FromWhereData fromWhere;
			if("".equals(dateTime)) {
				TimeConstant.dateTime = PLAdao.getHourTime("4.1", getDate());
				fromWhere = PLAdao.fromWhere("4.1", TimeConstant.dateTime);
			}else {
				fromWhere = PLAdao.fromWhere("4.1", dateTime);
			}
			if (fromWhere != null) {
				CommonMapData commonMapData1 = new CommonMapData();
				commonMapData1.setName("北京");
				commonMapData1.setValue(fromWhere.getSource1());
				list.add(commonMapData1);
				CommonMapData commonMapData2 = new CommonMapData();
				commonMapData2.setName("上海");
				commonMapData2.setValue(fromWhere.getSource2());
				list.add(commonMapData2);
				CommonMapData commonMapData3 = new CommonMapData();
				commonMapData3.setName("河北");
				commonMapData3.setValue(fromWhere.getSource3());
				list.add(commonMapData3);
				CommonMapData commonMapData4 = new CommonMapData();
				commonMapData4.setName("河南");
				commonMapData4.setValue(fromWhere.getSource4());
				list.add(commonMapData4);
				CommonMapData commonMapData5 = new CommonMapData();
				commonMapData5.setName("山东");
				commonMapData5.setValue(fromWhere.getSource5());
				list.add(commonMapData5);
				CommonMapData commonMapData6 = new CommonMapData();
				commonMapData6.setName("山西");
				commonMapData6.setValue(fromWhere.getSource6());
				list.add(commonMapData6);
				CommonMapData commonMapData7 = new CommonMapData();
				commonMapData7.setName("陕西");
				commonMapData7.setValue(fromWhere.getSource7());
				list.add(commonMapData7);
				CommonMapData commonMapData8 = new CommonMapData();
				commonMapData8.setName("湖南");
				commonMapData8.setValue(fromWhere.getSource8());
				list.add(commonMapData8);
				CommonMapData commonMapData9 = new CommonMapData();
				commonMapData9.setName("安徽");
				commonMapData9.setValue(fromWhere.getSource9());
				list.add(commonMapData9);
				CommonMapData commonMapData10 = new CommonMapData();
				commonMapData10.setName("湖北");
				commonMapData10.setValue(fromWhere.getSource10());
				list.add(commonMapData10);
			}

			FromWhereData fromWhere2;

			if("".equals(dateTime)) {
				fromWhere2 = PLAdao.fromWhere("4.2", TimeConstant.dateTime);
			}else{
				fromWhere2 = PLAdao.fromWhere("4.2", dateTime);
			}
			if (fromWhere2 != null) {
				CommonMapData commonMapData1 = new CommonMapData();
				commonMapData1.setName("北京");
				commonMapData1.setValue(fromWhere2.getSource1());
				list.add(commonMapData1);
				CommonMapData commonMapData2 = new CommonMapData();
				commonMapData2.setName("上海");
				commonMapData2.setValue(fromWhere2.getSource2());
				list.add(commonMapData2);
				CommonMapData commonMapData3 = new CommonMapData();
				commonMapData3.setName("河北");
				commonMapData3.setValue(fromWhere2.getSource3());
				list.add(commonMapData3);
				CommonMapData commonMapData4 = new CommonMapData();
				commonMapData4.setName("河南");
				commonMapData4.setValue(fromWhere2.getSource4());
				list.add(commonMapData4);
				CommonMapData commonMapData5 = new CommonMapData();
				commonMapData5.setName("山东");
				commonMapData5.setValue(fromWhere2.getSource5());
				list.add(commonMapData5);
				CommonMapData commonMapData6 = new CommonMapData();
				commonMapData6.setName("山西");
				commonMapData6.setValue(fromWhere2.getSource6());
				list.add(commonMapData6);
				CommonMapData commonMapData7 = new CommonMapData();
				commonMapData7.setName("陕西");
				commonMapData7.setValue(fromWhere2.getSource7());
				list.add(commonMapData7);
				CommonMapData commonMapData8 = new CommonMapData();
				commonMapData8.setName("湖南");
				commonMapData8.setValue(fromWhere2.getSource8());
				list.add(commonMapData8);
				CommonMapData commonMapData9 = new CommonMapData();
				commonMapData9.setName("安徽");
				commonMapData9.setValue(fromWhere2.getSource9());
				list.add(commonMapData9);
				CommonMapData commonMapData10 = new CommonMapData();
				commonMapData10.setName("湖北");
				commonMapData10.setValue(fromWhere2.getSource10());
				list.add(commonMapData10);
			}
		}else {

			FromWhereData fromWhere;

			if("".equals(dateTime)) {
				TimeConstant.dateTime = PLAdao.getHourTime(room, getDate());
				fromWhere = PLAdao.fromWhere(room, TimeConstant.dateTime);
			}else{
				fromWhere = PLAdao.fromWhere(room, dateTime);
			}

			if (fromWhere != null) {
				CommonMapData commonMapData1 = new CommonMapData();
				commonMapData1.setName("北京");
				commonMapData1.setValue(fromWhere.getSource1());
				list.add(commonMapData1);
				CommonMapData commonMapData2 = new CommonMapData();
				commonMapData2.setName("上海");
				commonMapData2.setValue(fromWhere.getSource2());
				list.add(commonMapData2);
				CommonMapData commonMapData3 = new CommonMapData();
				commonMapData3.setName("河北");
				commonMapData3.setValue(fromWhere.getSource3());
				list.add(commonMapData3);
				CommonMapData commonMapData4 = new CommonMapData();
				commonMapData4.setName("河南");
				commonMapData4.setValue(fromWhere.getSource4());
				list.add(commonMapData4);
				CommonMapData commonMapData5 = new CommonMapData();
				commonMapData5.setName("山东");
				commonMapData5.setValue(fromWhere.getSource5());
				list.add(commonMapData5);
				CommonMapData commonMapData6 = new CommonMapData();
				commonMapData6.setName("山西");
				commonMapData6.setValue(fromWhere.getSource6());
				list.add(commonMapData6);
				CommonMapData commonMapData7 = new CommonMapData();
				commonMapData7.setName("陕西");
				commonMapData7.setValue(fromWhere.getSource7());
				list.add(commonMapData7);
				CommonMapData commonMapData8 = new CommonMapData();
				commonMapData8.setName("湖南");
				commonMapData8.setValue(fromWhere.getSource8());
				list.add(commonMapData8);
				CommonMapData commonMapData9 = new CommonMapData();
				commonMapData9.setName("安徽");
				commonMapData9.setValue(fromWhere.getSource9());
				list.add(commonMapData9);
				CommonMapData commonMapData10 = new CommonMapData();
				commonMapData10.setName("湖北");
				commonMapData10.setValue(fromWhere.getSource10());
				list.add(commonMapData10);
			}
		}
		return getMessage(list,"fromWhere");
	}

	/**
	 * 发送场馆来源
	 * @param room
	 */
	public void sendFromWhere(String room,String dataTime){
		sendMessage(fromWhere(room,dataTime));
	}
	
	
	/**
	 * 性别统计
	 * room = -1所有
	 * @return
	 */
	public String sexStatic(String room,String dataTime){
		List<CommonMapData> list = new ArrayList<CommonMapData>();
		if("-1".equals(room)){
			SexMapData sexdata;
			if("".equals(dataTime)) {
				TimeConstant.dateTime = PLAdao.getHourTime("4.1", getDate());
				sexdata = PLAdao.sexStatic("4.1", TimeConstant.dateTime);
			}else {
				sexdata = PLAdao.sexStatic("4.1", dataTime);
			}

			CommonMapData commonMapData = new CommonMapData();
			commonMapData.setName("女");
			commonMapData.setValue(sexdata.getFemale());
			list.add(commonMapData);
			CommonMapData commonMapData1 = new CommonMapData();
			commonMapData1.setName("男");
			commonMapData1.setValue(sexdata.getMale());
			list.add(commonMapData1);

			SexMapData sexdata2;
			if("".equals(dataTime)) {
				sexdata2 = PLAdao.sexStatic("4.2", TimeConstant.dateTime);
			}else{
				sexdata2 = PLAdao.sexStatic("4.2", dataTime);
			}

			CommonMapData commonMapData2 = new CommonMapData();
			commonMapData2.setName("女");
			commonMapData2.setValue(sexdata2.getFemale());
			list.add(commonMapData2);
			CommonMapData commonMapData3 = new CommonMapData();
			commonMapData3.setName("男");
			commonMapData3.setValue(sexdata2.getMale());
			list.add(commonMapData3);

		}else {
			SexMapData sexdata;
			if("".equals(dataTime)) {
				TimeConstant.dateTime = PLAdao.getHourTime(room, getDate());
				sexdata = PLAdao.sexStatic(room, TimeConstant.dateTime);
			}else{
				sexdata = PLAdao.sexStatic(room, dataTime);
			}

			CommonMapData commonMapData = new CommonMapData();
			commonMapData.setName("女");
			commonMapData.setValue(sexdata.getFemale());
			list.add(commonMapData);
			CommonMapData commonMapData1 = new CommonMapData();
			commonMapData1.setName("男");
			commonMapData1.setValue(sexdata.getMale());
			list.add(commonMapData1);
		}
		return getMessage(list,"sexStatic");
	}

	/**
	 * 发送性别统计
	 *
	 */
	public void sendSexStatic(String room,String dateTime){
		sendMessage(sexStatic(room,dateTime));
	}

	/**
	 * app统计
	 * room = -1所有
	 * @return
	 */
	public String appStatic(String room){
		List<CommonMapData> list = new ArrayList<CommonMapData>();
		CommonMapData commonMapData = new CommonMapData();
		commonMapData.setName("微博");
		commonMapData.setValue((int)(Math.random()*100));
		list.add(commonMapData);
		CommonMapData commonMapData1 = new CommonMapData();
		commonMapData1.setName("微信");
		commonMapData1.setValue((int)(Math.random()*100));
		list.add(commonMapData1);
		CommonMapData commonMapData2 = new CommonMapData();
		commonMapData2.setName("QQ");
		commonMapData2.setValue((int)(Math.random()*100));
		list.add(commonMapData2);
		CommonMapData commonMapData3 = new CommonMapData();
		commonMapData3.setName("陌陌");
		commonMapData3.setValue((int)(Math.random()*100));
		list.add(commonMapData3);
		CommonMapData commonMapData4 = new CommonMapData();
		commonMapData4.setName("探探");
		commonMapData4.setValue((int)(Math.random()*100));
		list.add(commonMapData4);
		CommonMapData commonMapData5 = new CommonMapData();
		commonMapData5.setName("私聊");
		commonMapData5.setValue((int)(Math.random()*100));
		list.add(commonMapData5);
		CommonMapData commonMapData6 = new CommonMapData();
		commonMapData6.setName("Facebook");
		commonMapData6.setValue((int)(Math.random()*100));
		list.add(commonMapData6);
		return getMessage(list,"appStatic");
	}

	/**
	 * 发送app统计
	 * @param room
	 */
	public void sendAppStatic(String room){
		sendMessage(appStatic(room));
	}
	
	/**
	 * 年龄分布
	 * room = -1所有
	 * @return
	 */
	public String agesStatic(String room,String dateTime){
		List<CommonMapData> list = new ArrayList<CommonMapData>();
		if ("-1".equals(room)) {
			AgeMapData ageData;
			if("".equals(dateTime)) {
				TimeConstant.dateTime = PLAdao.getHourTime("4.1", getDate());
				ageData = PLAdao.agesStatic("4.1", TimeConstant.dateTime);
			}else{
				ageData = PLAdao.agesStatic("4.1", dateTime);
			}
			CommonMapData commonMapData1 = new CommonMapData();
			commonMapData1.setName("0-12");
			commonMapData1.setValue(ageData.getAge1());
			list.add(commonMapData1);
			CommonMapData commonMapData2 = new CommonMapData();
			commonMapData2.setName("12-18");
			commonMapData2.setValue(ageData.getAge2());
			list.add(commonMapData2);
			CommonMapData commonMapData3 = new CommonMapData();
			commonMapData3.setName("18-25");
			commonMapData3.setValue(ageData.getAge3());
			list.add(commonMapData3);
			CommonMapData commonMapData4 = new CommonMapData();
			commonMapData4.setName("25-35");
			commonMapData4.setValue(ageData.getAge4());
			list.add(commonMapData4);
			CommonMapData commonMapData5 = new CommonMapData();
			commonMapData5.setName("35-45");
			commonMapData5.setValue(ageData.getAge5());
			list.add(commonMapData5);
			CommonMapData commonMapData6 = new CommonMapData();
			commonMapData6.setName("45-55");
			commonMapData6.setValue(ageData.getAge6());
			list.add(commonMapData6);
			CommonMapData commonMapData7 = new CommonMapData();
			commonMapData7.setName("55-65");
			commonMapData7.setValue(ageData.getAge1());
			list.add(commonMapData7);
			CommonMapData commonMapData8 = new CommonMapData();
			commonMapData8.setName("65-80");
			commonMapData8.setValue(ageData.getAge8());
			list.add(commonMapData8);

			AgeMapData ageData2;
			if("".equals(dateTime)) {
				ageData2 = PLAdao.agesStatic("4.2", TimeConstant.dateTime);
			}else{
				ageData2 = PLAdao.agesStatic("4.2", dateTime);
			}
			CommonMapData commonMapData9 = new CommonMapData();
			commonMapData9.setName("0-12");
			commonMapData9.setValue(ageData2.getAge1());
			list.add(commonMapData9);
			CommonMapData commonMapData10= new CommonMapData();
			commonMapData10.setName("12-18");
			commonMapData10.setValue(ageData2.getAge2());
			list.add(commonMapData10);
			CommonMapData commonMapData11 = new CommonMapData();
			commonMapData11.setName("18-25");
			commonMapData11.setValue(ageData2.getAge3());
			list.add(commonMapData11);
			CommonMapData commonMapData12 = new CommonMapData();
			commonMapData12.setName("25-35");
			commonMapData12.setValue(ageData2.getAge4());
			list.add(commonMapData12);
			CommonMapData commonMapData13 = new CommonMapData();
			commonMapData13.setName("35-45");
			commonMapData13.setValue(ageData2.getAge5());
			list.add(commonMapData13);
			CommonMapData commonMapData14 = new CommonMapData();
			commonMapData14.setName("45-55");
			commonMapData14.setValue(ageData2.getAge6());
			list.add(commonMapData14);
			CommonMapData commonMapData15 = new CommonMapData();
			commonMapData15.setName("55-65");
			commonMapData15.setValue(ageData2.getAge1());
			list.add(commonMapData15);
			CommonMapData commonMapData16 = new CommonMapData();
			commonMapData16.setName("65-80");
			commonMapData16.setValue(ageData2.getAge8());
			list.add(commonMapData16);
		}else {
			AgeMapData ageData;
			if("".equals(dateTime)) {
				TimeConstant.dateTime = PLAdao.getHourTime(room, getDate());
				ageData = PLAdao.agesStatic(room, TimeConstant.dateTime);
			}else{
				ageData = PLAdao.agesStatic(room, dateTime);
			}
			CommonMapData commonMapData1 = new CommonMapData();
			commonMapData1.setName("0-12");
			commonMapData1.setValue(ageData.getAge1());
			list.add(commonMapData1);
			CommonMapData commonMapData2 = new CommonMapData();
			commonMapData2.setName("12-18");
			commonMapData2.setValue(ageData.getAge2());
			list.add(commonMapData2);
			CommonMapData commonMapData3 = new CommonMapData();
			commonMapData3.setName("18-25");
			commonMapData3.setValue(ageData.getAge3());
			list.add(commonMapData3);
			CommonMapData commonMapData4 = new CommonMapData();
			commonMapData4.setName("25-35");
			commonMapData4.setValue(ageData.getAge4());
			list.add(commonMapData4);
			CommonMapData commonMapData5 = new CommonMapData();
			commonMapData5.setName("35-45");
			commonMapData5.setValue(ageData.getAge5());
			list.add(commonMapData5);
			CommonMapData commonMapData6 = new CommonMapData();
			commonMapData6.setName("45-55");
			commonMapData6.setValue(ageData.getAge6());
			list.add(commonMapData6);
			CommonMapData commonMapData7 = new CommonMapData();
			commonMapData7.setName("55-65");
			commonMapData7.setValue(ageData.getAge1());
			list.add(commonMapData7);
			CommonMapData commonMapData8 = new CommonMapData();
			commonMapData8.setName("65-80");
			commonMapData8.setValue(ageData.getAge8());
			list.add(commonMapData8);
		}
		return getMessage(list,"agesStatic");
	}

	/**
	 * 发送年龄分布
	 * @param room
	 */
	public void sendAgesStatic(String room,String dateTime){
		sendMessage(agesStatic(room,dateTime));
	}
	

	/**
	 * 历史人流统计
	 * room为-1表示所有场馆
	 * 按小时汇总数据
	 * @return
	 */
	public String personNumHisStatic(String room,String startTime,String endTime){
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		List<HistoryFlowData> historyFlowDataList = new ArrayList<HistoryFlowData>();
		List<personNumHisData> list_hf = PLAdao.personNumHisStatic(room, startTime==null?getTime():startTime, endTime==null?format.format(new Date()):endTime);
		Set set = new HashSet();

		for(personNumHisData personNumHisData:list_hf){
			set.add(personNumHisData.getTime());
		}

		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {
			HistoryFlowData historyFlowData = new HistoryFlowData();
			String time = iterator.next().toString();
			List<HistoryFlow> list = new ArrayList<HistoryFlow>();
			for (personNumHisData personNumHisData : list_hf) {
				if (personNumHisData.getTime().equals(time)) {
					HistoryFlow historyFlow = new HistoryFlow();
					historyFlow.setHour(Integer.parseInt(personNumHisData.getHour()));
					historyFlow.setAllUserNub(personNumHisData.getNum());
					list.add(historyFlow);
				}
			}
			historyFlowData.setTime(time);
			historyFlowData.setDatas(list);
			historyFlowDataList.add(historyFlowData);
		}

		return getMessage(historyFlowDataList,"personNumHisStatic");
	}

	/**
	 * 发送历史人流统计
	 * room为-1表示所有场馆
	 * 按小时汇总数据
	 * @return
	 */
	public void sendPersonNumHisStatic(String room,String startTime,String endTime){
		sendMessage(personNumHisStatic(room,startTime,endTime));
	}
	
	/**
	 * 人流量预警
	 * room = -1 表示所有
	 * @return
	 */
	public String personNumWarn(String room){
		List<RoomWorn> list = new ArrayList<RoomWorn>();
		if("-1".equals(room)) {
			RoomWorn rm = new RoomWorn();
			rm.setRoomCode("4.1");
			rm.setVenue(25);
			rm.setGridVlue(20);

			RoomWorn rm1 = new RoomWorn();
			rm1.setRoomCode("4.2");
			rm1.setVenue(25);
			rm1.setGridVlue(20);

			list.add(rm);
			list.add(rm1);
		}else if("4.1".equals(room)){
			RoomWorn rm = new RoomWorn();
			rm.setRoomCode("4.1");
			rm.setVenue(25);
			rm.setGridVlue(20);
			list.add(rm);
		}else if("4.2".equals(room)){
			RoomWorn rm1 = new RoomWorn();
			rm1.setRoomCode("4.2");
			rm1.setVenue(25);
			rm1.setGridVlue(20);
			list.add(rm1);
		}

		return getMessage(list,"personNumWarn");
	}

	/**
	 * 发送人流量预警
	 * room = -1 表示所有
	 * @return
	 */
	public void sendPersonNumWarn(String room){
		sendMessage(personNumWarn(room));
	}
	
	/***
	 * 拼接返回json
	 * @return
	 */
	public static void sendMessage(String message){
		CopyOnWriteArraySet<PLAwebSocket>  webSocketSet = PLAwebSocket.webSocketSet;
		for(PLAwebSocket item: webSocketSet){
			try {
				item.sendMessage(message);
			} catch (IOException e) {
				e.printStackTrace();
				continue;
			}
		}
	}
	
	/***
	 * 拼接返回json
	 * @return
	 */
	public static String getMessage(List data,String method){
		ErrorMapData errorMapData = new ErrorMapData(0,"");
		Response response = new Response("success",errorMapData,method,data);
		String message = BeanJsonUtil.bean2Json(response);
		return message;
	}

}

